/*
 * Copyright 2018 Zhongan.com All right reserved. This software is the
 * confidential and proprietary information of Zhongan.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Zhongan.com.
 */

package com.zhongan.cloud.gateway.db;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.taobao.tddl.client.jdbc.TDataSource;

/**
 * TDDL数据源配置
 *
 * @author tongyufu
 * @since 2018年7月3日 下午5:24:55
 */
@Configuration
public class TddlConfig {

    @Primary
    @Bean(initMethod = "init")
    public TDataSource dataSource() {
        TDataSource dataSource = new TDataSource();
        dataSource.setAppName("TAC_GW_TST_APP");
        dataSource.setDynamicRule(true);
        return dataSource;
    }

    @Bean
    public DataSourceTransactionManager transactionManager(TDataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}
